package com.tenement.controller;

import com.mybatisflex.core.paginate.Page;
import com.tenement.common.Axios;
import com.tenement.enums.RespBusiness;
import com.tenement.netty.Handler.ChatHandler;
import com.tenement.vo.QueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import com.tenement.entity.Chat;
import com.tenement.service.ChatService;
import org.springframework.web.bind.annotation.RestController;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *  控制层。
 *
 * @author BertXie
 * @since 1.0.1
 */
@RestController
@RequestMapping("/chat")
@RequiredArgsConstructor
@Api(tags = "聊天记录")
public class ChatController {

    private final ChatService chatService;


    @ApiOperation("分页查询")
    @PostMapping("list")
    public Axios list(@RequestBody QueryVo queryVo) {
       return Axios.success(chatService.list(queryVo));
    }

    @ApiOperation("删除根据id")
    @DeleteMapping("removeById")
    public Axios removeById(@RequestBody List<Serializable> ids) {
        return chatService.removeByIds(ids) ? Axios.success(RespBusiness.SUCCESS) : Axios.error(RespBusiness.FAIL);
    }

    @ApiOperation("删除根据用户")
    @DeleteMapping("removeByUser/{userId}/{targetId}")
    public Axios removeByUser(@PathVariable Long userId, @PathVariable Long targetId) {
        return chatService.removeByUser(userId, targetId)  > 0? Axios.success(RespBusiness.SUCCESS) : Axios.error(RespBusiness.FAIL);
    }

    @ApiOperation("在线人数统计")
    @GetMapping("onlineCount")
    public Axios onlineCount() {
        Map<String, Object> res = new HashMap<>();
        res.put("onlineCount", ChatHandler.privateChannel.size());
        return Axios.success(res);
    }

}
